package wyq.gy;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;

import java.util.ArrayList;
import java.util.List;

import wyq.gy.Bean.historyOrder;
import wyq.gy.Tool.util;
import wyq.gy.adapter.HistoryOrderAdapter;

public class HistoryOrderActivity extends AppCompatActivity {
    private List<historyOrder> datalist;
    private ListView mlistView;
    private HistoryOrderAdapter mAdapter;
    private String m_state;
    private String m_title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_order);

        m_state = getIntent().getStringExtra("state");
        mlistView = (ListView) findViewById(R.id.historyOrderList);
        datalist = new ArrayList<>();
        mAdapter = new HistoryOrderAdapter(HistoryOrderActivity.this, datalist);
        mlistView.setAdapter(mAdapter);
        mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                historyOrder data = datalist.get(position);
                Intent intent = new Intent();
                intent.putExtra("orderId",data.getId());
                intent.setClass(HistoryOrderActivity.this, OrderDetailActivity.class);
                startActivity(intent);
            }
        });
        getData();

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setTitle(m_title);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void getData() {
        datalist.clear();
        if (util.isNetworkAvailable(this.getApplicationContext())) {
            AVQuery<AVObject> query = new AVQuery<>("MyOrderList");
            AVUser currentUser = AVUser.getCurrentUser();
            query.orderByAscending("Status");
            if(m_state.equals("4")) {
                query.whereEqualTo("Status", "4");
                m_title = "历史订单";
            }else{
                query.whereLessThan("Status", "4");
                m_title = "未完成订单";
            }
            query.whereEqualTo("EmployerID", currentUser.getUsername());//通过关键字查找表单
            query.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> listorder, AVException e) {
                for (AVObject avObject : listorder) {
                    historyOrder order = new historyOrder(avObject.getObjectId(),avObject.getString("RepairCategory"),util.GetStatusText(avObject.getString("Status")),avObject.getString("HandleTime"),avObject.getString("RealAddress"),avObject.getString("ServiceContent"));
                    datalist.add(order);
                }
                mAdapter.notifyDataSetChanged();
                }
            });

        } else {
            Toast.makeText(this, "获取数据失败", Toast.LENGTH_SHORT).show();
        }
    }


}
